var vue = new Vue({
    el : '#app',
    data : {
        roleTreeData : [],
        treeProps: {
            children: 'children',
            label: 'roleName'
        },
        tableData: [],
        loading: false,

        roleFormVisible: false,
        formRead: false,
        roleId : 0,
        roleForm: {
            roleName : '',
            remark : '',
        },
        roleFormRules: {
            roleName: [
                {required: true, message: '请填写角色名称'},
            ]
        },
    },
    created : function(){
        this.loadAllRole();
    },
    methods : {
        loadAllRole: function () {
            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/role/list' ,
                success: function (result) {
                    if (result.code === 200) {
                        self.tableData = result.body;
                    }
                }
            })
        },
        saveRole : function(){
            var self = this;
            self.$refs['roleForm'].validate(function (valid) {
                if (!valid) return false;

                $.ajax({
                    url: constant.ROOT_SITE + '/system/role/save',
                    data: JSON.stringify(self.roleForm),
                    success: function (result) {
                        if (result.code === 200) {
                            self.$message({
                                type: 'success',
                                message: '保存成功！'
                            });
                            self.roleFormVisible = false;
                            self.loadAllRole();
                        } else {
                            self.$message.error(result.errMsg);
                        }
                    }
                })

            });
        },
        //删除
        deleteRole : function(role){
            var self = this;

            this.$confirm('您确定用删除该角色吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                $.ajax({
                    url:  constant.ROOT_SITE + '/system/role/delete/'+role.id,
                    success: function (result) {
                        if (result.code === 200 && result.body) {
                            self.$message({
                                type: 'success',
                                message: '删除成功！'
                            });
                            self.loadAllRole();
                        }
                    }
                })
            });


        },
        updateBusinessStatus:function(row){
            var self = this;
            var form = {
                id : row.id,
                businessFlag:!row.businessFlag,
            }
            $.ajax({
                url: constant.ROOT_SITE + '/system/role/save',
                data: JSON.stringify(form),
                success: function (result) {
                    if (result.code === 200) {
                        self.$message({
                            type: 'success',
                            message: '操作成功！'
                        });
                        row.businessFlag = !row.businessFlag;
                    } else {
                        self.$message.error(result.errMsg);
                    }
                }
            })
        },
        showRoleDialog : function(){
            this.roleFormVisible = true;
        },
        showEditRoleDialog : function(role){
            $.extend(this.roleForm, role);
            this.roleFormVisible = true;
        },
        handleNodeClick : function(role){
            this.loadAllRole(role.id);
        },
        handleDialogClose :function () {
            this.roleForm={
                roleName : '',
                remark : '',
            };
        }
    }
});